import java.util.*;
import java.util.stream.Collectors;

/**
 * 2512. 奖励最顶尖的 K 名学生
 */
public class No2512 {
    public List<Integer> topStudents(String[] positive_feedback, String[] negative_feedback, String[] report, int[] student_id, int k) {
        Set<String> positiveList = Arrays.stream(positive_feedback).collect(Collectors.toSet());
        Set<String> negativeList = Arrays.stream(negative_feedback).collect(Collectors.toSet());
        int n = report.length;
        int[][] treeMap = new int[n][2];
        for (int i = 0; i < n; i++) {
            int curScore = 0;
            for (String word : report[i].split(" ")) {
                if (positiveList.contains(word)) curScore += 3;
                else if (negativeList.contains(word)) curScore -= 1;
            }
            treeMap[i][0] = student_id[i];
            treeMap[i][1] = curScore;
        }
        Arrays.sort(treeMap, (e1, e2) -> (e2[1] - e1[1] == 0 ? e1[0] - e2[0] : e2[1] - e1[1]));
        List<Integer> ans = new ArrayList<>(k);
        for (int i = 0; i < k; i++) {
            ans.add(treeMap[i][0]);
        }
        return ans;
    }
}
